import { Context } from 'koa'
import roleService from '../services/role'

export class RoleController {
  // 创建角色
  async create(ctx: Context) {
    const roleData = ctx.request.body
    const role = await roleService.create(roleData)
    ctx.status = 201
    ctx.body = {
      success: true,
      data: role,
      message: '角色创建成功'
    }
  }

  // 更新角色
  async update(ctx: Context) {
    const { id } = ctx.params
    const updateData = ctx.request.body
    const role = await roleService.update(parseInt(id), updateData)
    ctx.status = 200
    ctx.body = {
      success: true,
      data: role,
      message: '角色更新成功'
    }
  }

  // 删除角色
  async delete(ctx: Context) {
    const { id } = ctx.params
    await roleService.delete(parseInt(id))
    ctx.status = 200
    ctx.body = {
      success: true,
      message: '角色删除成功'
    }
  }
  
  // 获取角色列表
  async findAll(ctx: Context) {
    const roles = await roleService.findAll(ctx.query)
    ctx.status = 200
    ctx.body = {
      success: true,
      data: roles,
      message: '查询成功'
    }
  }

  // 查询单个角色
  async findOne(ctx: Context) {
    const { id } = ctx.params
    const role = await roleService.findOne(parseInt(id))
    ctx.status = 200
    ctx.body = {
      success: true,
      data: role,
      message: '查询成功'
    }
  }
}

export const roleController = new RoleController()
